﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OutlookLiveDomain.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands used to manage outlook live domain.
    /// </summary>
    public class OutlookLiveDomain
    {
        /// <summary>
        /// Session runspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OutlookLiveDomain class.
        /// </summary>
        /// <param name="sessionRunspace">Session runspace instance.</param>
        public OutlookLiveDomain(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Disables Instant Messaging feature
        /// </summary>
        /// <param name="identity">windows live id</param>
        /// <returns>true if success</returns>
        public bool DisableInstantMessaging(string identity)
        {
            if (string.IsNullOrEmpty(identity))
            {
                throw new ArgumentOutOfRangeException("identity");
            }

            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(DomainScripts.DisableInstantMessagingScript, identity));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Disables Text Messaging feature 
        /// </summary>
        /// <param name="identity">windows live id</param>
        /// <returns>true if success</returns>
        public bool DisableTextMessaging(string identity)
        {
            if (string.IsNullOrEmpty(identity))
            {
                throw new ArgumentOutOfRangeException("identity");
            }

            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(DomainScripts.DisableTextMessagingScript, identity));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Enables Instant Messaging feature
        /// </summary>
        /// <param name="identity">windows live id</param>
        /// <returns>true if success</returns>
        public bool EnableInstantMessaging(string identity)
        {
            if (string.IsNullOrEmpty(identity))
            {
                throw new ArgumentOutOfRangeException("identity");
            }

            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(DomainScripts.EnableInstantMessagingScript, identity));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Enables Text Messaging feature 
        /// </summary>
        /// <param name="identity">windows live id</param>
        /// <returns>true if success</returns>
        public bool EnableTextMessaging(string identity)
        {
            if (string.IsNullOrEmpty(identity))
            {
                throw new ArgumentOutOfRangeException("identity");
            }

            try
            {
                var command = new PSCommand();
                command.AddScript(string.Format(DomainScripts.EnableTextMessagingScript, identity));
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Fetch mailbox policies.
        /// </summary>
        /// <param name="identity">windows live id</param>
        /// <returns>Current mailbox policy.</returns>
        public MailBoxPolicies GetMailBoxPolicies(string identity)
        {
            if (string.IsNullOrEmpty(identity))
            {
                throw new ArgumentOutOfRangeException("identity");
            }

            var command = new PSCommand();
            command.AddScript(string.Format(DomainScripts.GetMailboxPolicyScript, identity));
            var policy = this.sessionRunspace.ExecuteCommand(command);

            if (policy != null && policy.Count > 0)
            {
                return new MailBoxPolicies(
                    (bool)policy[0].Members[Constants.InstantMessagingEnabled].Value,
                    (bool)policy[0].Members[Constants.TextMessagingEnabled].Value);
            }

            return new MailBoxPolicies(false, false);
        }

        /// <summary>
        /// Method sets rule for bad words filter
        /// </summary>
        /// <param name="wordList">List of bad words</param>
        /// <returns>True if success, false otherwise.</returns>
        public bool BlockBadWords(string wordList)
        {
            try
            {
                var command = new PSCommand();

                // By default disable bad words blocker
                command.AddScript(DomainScripts.DisableBlockBadWordsScript);

                // remove the , at the end of string
                if (!string.IsNullOrEmpty(wordList))
                {
                    if (wordList.LastIndexOf(',') == (wordList.Length - 1))
                    {
                        wordList = wordList.Remove(wordList.LastIndexOf(','));
                    }

                    // Enable bad words blocker with the list of bad words
                    command.AddScript(string.Format(DomainScripts.BlockBadWordsScript, wordList));
                }

                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        ///  Method to get blocked bad words
        /// </summary>
        /// <returns>string containing comma seperated bad words</returns>
        public string ListBadWords()
        {
            string words = string.Empty;

            try
            {
                var command = new PSCommand();
                command.AddScript(DomainScripts.ListBadWordsScript);
                var results = this.sessionRunspace.ExecuteCommand(command);
                
                if ((bool) results[0].Members[Constants.BadWordsPolicyEnabled].Value)
                {
                    words = results[0].Members[Constants.BadWordsList].Value.ToString();
                }
            }
            catch
            {
                // Ignore the exception
            }

            return words;
        }
    }
}
